package com.example.controller;

import com.example.domain.Book;
import com.example.domain.User;
import com.example.entity.PageResult;
import com.example.entity.Result;
import com.example.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping(value = {"/selectNewbooks"})
    public ModelAndView selectNewBooks() {

        ModelAndView modelAndView = new ModelAndView();

        // 数据和视图
        int pageNum = 1;
        int pageSize = 10;
        PageResult pageResult = bookService.selectNewBooks(pageNum, pageSize);

        modelAndView.addObject("pageResult", pageResult);
        modelAndView.setViewName("books_new"); // 跳转到 books_new.jsp 页面

        return modelAndView;
    }

    @RequestMapping(value = {"/findById"})
    @ResponseBody
    public Result<Book> findBookById(String id) {
        Book book = bookService.findBookById(id);
        return new Result<>(true, "", book);
    }



    @ResponseBody
    @RequestMapping(value = {"/borrowBook"})
    public Result borrowBook(Book book, HttpSession session) {

        // 设置借阅人
        User user = (User) session.getAttribute("USER_SESSION");
        book.setBorrower(user.getName());

        // 调用 service
        Integer num = bookService.borrowBook(book);
        if (num > 0) {
            return new Result(true, "借阅成功");
        } else {
            return new Result(true, "借阅失败");
        }

    }



    @ResponseBody
    @RequestMapping(value = {"/search"})
    public ModelAndView search(Book book, Integer pageNum, Integer pageSize, HttpServletRequest request) {

        // 调用service
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        PageResult pageResult = bookService.search(book, pageNum, pageSize);

        // 页面
        ModelAndView modelAndView = new ModelAndView("books");

        // 数据
        // 搜索框数据回显
        modelAndView.addObject("search", book);
        // 分页信息
        modelAndView.addObject("pageResult", pageResult);
        // 当前页码数
        modelAndView.addObject("pageNum", pageNum);
        // 分页请求再次跳转的url
        modelAndView.addObject("gourl", request.getRequestURI());

        return modelAndView;
    }


    @ResponseBody
    @RequestMapping(value = {"/searchBorrowed"})
    public ModelAndView searchBorrowed(Book book, Integer pageNum, Integer pageSize, HttpServletRequest request) {

        // 调用service
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        User user = (User) request.getSession().getAttribute("USER_SESSION");
        PageResult pageResult = bookService.searchBorrowed(book, user, pageNum, pageSize);

        // 页面
        ModelAndView modelAndView = new ModelAndView("book_borrowed");

        // 数据
        // 搜索框数据回显
        modelAndView.addObject("search", book);
        // 分页信息
        modelAndView.addObject("pageResult", pageResult);
        // 当前页码数
        modelAndView.addObject("pageNum", pageNum);
        // 分页请求再次跳转的url
        modelAndView.addObject("gourl", request.getRequestURI());

        return modelAndView;

    }
    /*
     24/44
      借阅信息
     */


    @ResponseBody
    @RequestMapping(value = {"/addBook"})
    public Result addBook(Book book) {

        Integer num = bookService.addBook(book);

        if (num > 0) {
            // 成功
            return new Result<>(true, "新增图书成功");
        } else {
            // 失败
            return new Result<>(true, "新增图书失败");
        }

    }

    @ResponseBody
    @RequestMapping(value = {"/editBook"})
    public Result editBook(Book book) {

        Integer num = bookService.editBook(book);

        if (num > 0) {
            return new Result<>(true, "修改图书成功");
        } else {
            return new Result<>(true, "修改图书失败");
        }
    }


    @ResponseBody
    @RequestMapping(value = {"/returnBook"})
    public Result returnBook(String id, HttpSession session) {

        User user = (User) session.getAttribute("USER_SESSION");

        try {
            Boolean flag = bookService.returnBook(id, user);
            if (flag) {
                return new Result<>(true, "归还成功,请到行政中心还书");
            } else {
                return new Result<>(true, "归还失败");
            }
        } catch (Exception e) {
            return new Result<>(false, "归还失败");
        }

    }


    @ResponseBody
    @RequestMapping(value = {"/returnConfirm"})
    public Result returnConfirmed(String id){

        Boolean flag =bookService.confirmed(id);

        if (flag) {
            return new Result<>(true, "确认成功");
        } else {
            return new Result<>(true, "确认失败");
        }

    }

}














